/********************************************************************************
 * Copyright(c) 2020-2025 VINCENT_WY All rights reserved.
 * - Filename
 * - Author  Vincent
 * - Version V1.0.0
 * - Date    2022/10/11
 * - Brief
 * - FunctionList:
 ******************************************************************************
 * History:
 *
 *
 *
 ******************************************************************************
 */
#include <iostream>
#include <string>
#include <fstream>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/time.h>
#include "lpc/LidarFrame.pb.h"
#include "google/protobuf/util/delimited_message_util.h"
#include "google/protobuf/util/json_util.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
/* parama define describe -----------------------------------------------------*/
using namespace std;
using namespace google::protobuf::util;
using namespace google::protobuf::io;
uint64_t getCurrenTimeMs();
/* class describe -----------------------------------------------------*/
int main(int argc, char *argv[])
{
    const char *dir_name = "LidarPerception/";
    int result = mkdir(dir_name, 0777);
    if (result == -1)
    {
        // 如果创建目录失败，根据errno的值输出错误信息
        if (errno == EEXIST)
        {
            std::cerr << "目录已存在。" << std::endl;
        }
        else
        {
            std::cerr << "创建目录时出错：" << errno << std::endl;
            return 0;
        }
    }
    else
    {
        std::cout << "目录创建成功。" << std::endl;
    }

    lpc::LidarProtoPointCloud protoFrame;
    const char *filename = argv[1];
    std::string fileNameStr = filename;
    std::cout << "input file: " << filename << std::endl;
    std::fstream input(filename, std::ios::in | std::ios::binary);
    if (!input)
    {
        return -1;
    }
    // uint64_t time1 = getCurrenTimeMs();

    std::ifstream m_dbFile;
    std::shared_ptr<google::protobuf::io::IstreamInputStream> _inDbStream{nullptr};

    m_dbFile.open(fileNameStr, std::ios::in | std::ios::binary);
    _inDbStream = std::make_shared<google::protobuf::io::IstreamInputStream>(&m_dbFile);

    bool clean_eof{false};
    bool ok{false};
    do
    {
        auto frame = std::make_shared<lpc::LidarPerceptionFrame>();
        ok = google::protobuf::util::ParseDelimitedFromZeroCopyStream(frame.get(), _inDbStream.get(), &clean_eof);
        std::cout << "time: " << frame->timestamp() << " count:" << std::to_string(frame->objects().size()) << std::endl;
        string json_str;
        JsonPrintOptions options;
        options.add_whitespace = true; // 设置格式化输出，添加空格让JSON更易读
        options.always_print_primitive_fields = true;
        Status status = MessageToJsonString(*frame, &json_str, options);
        if (status.ok())
        {
            std::string fileName = dir_name + std::to_string(frame->timestamp()) + ".json";
            ofstream outfile(fileName, ios::out);
            if (outfile.is_open())
            {
                outfile << json_str;
                outfile.close();
                cout << "JSON数据已成功写入" << fileName << endl;
            }
            else
            {
                cerr << "无法打开文件 " << fileName << endl;
                continue;
            }
        }
        else
        {
            cerr << "转换JSON失败: " << status.error_message() << endl;
        }

    } while (ok && !clean_eof);
    return 0;
}

uint64_t getCurrenTimeMs()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
/************************ (.cpp) END OF FILE ************************************/